#ifndef _QUEUE_HPP_
#define _QUEUE_HPP_

typedef int Queue_elem_type;


class Queue;
class Queue_elem
{
	friend class Queue;
	private:
		Queue_elem_type data;
		Queue_elem *next,*prev;
	public:
		Queue_elem();
};



class Queue
{
	private:
		Queue_elem 		*_first, *_last;
		unsigned int 	_size;
	public:
		Queue();
		~Queue();		
	////////////////
		void del();
		bool get( Queue_elem_type* elem);
		bool add( Queue_elem_type  elem);
		unsigned int  size();
};

/////////////////////////
Queue_elem::Queue_elem()
{
	next = 0;
	prev = 0;
}
/////////////////////////
Queue::Queue()
{
	_size = 0;
}
Queue::~Queue()
{
	Queue_elem_type tmp;
	while ( get( &tmp));
}
////////////////
bool Queue::get( Queue_elem_type* elem)
{
	if ( _size == 0)
	{
		return false;
	}
	if ( _size == 1)
	{
		*elem = _first->data;
		
		delete _first;
		_first = 0;
		_size--;
		return true;
	}
	_size--;
	
	Queue_elem *old_first = _first;
	
	*elem = _first->data;
	
	_first = _first->next;
	_first->prev = 0;
	
	delete old_first;
	return true;
}
bool Queue::add( Queue_elem_type  elem)
{
	Queue_elem *new_elem  = new Queue_elem();
	if ( new_elem == 0)
	{
		return false;
	}
	if ( _size == 0)
	{
		new_elem->data = elem;
		
		_last        = _first = new_elem;
		_last ->next = _last ->prev = 0;
		_first->next = _first->prev = 0;
		
		_size++;
		return true;
	}
	_size++;
	
	new_elem -> data = elem;
	
	new_elem ->next = 0;
	
	new_elem ->prev = _last;
	
	_last->next = new_elem;
	
	_last = new_elem;
	return true;
}
void Queue::del()
{
	Queue_elem_type tmp;
	while ( get( &tmp));
}

unsigned int  Queue::size()
{
	return _size;
}



#endif